<?php

include_once "src/controller/dbmanagement/AdminDBManager.php";
include_once "src/controller/regmanagement/EmailManager.php";
include_once "src/controller/regmanagement/AccountManager.php";
include_once "src/regexception/DBConnectionException.php";
include_once "src/regexception/DBException.php";
include_once "src/regexception/GeneralRegistryException.php";
include_once "src/model/RegistryCount.php";
include_once "src/controller/regmanagement/MiscellaneousManager.php";

class AdminManager {

    public static function isAdminLogInValid($email, $password) {
        $isAdminLogInValid = false;
        try {
            $isAdminLogInValid = AdminDBManager::isAdminLogInValid($email, $password);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $isAdminLogInValid;
    }

    public static function updateUserAccount($consultantId, $oldEmail, $newEmail,
                                             $newFirstName, $newLastName, $newPassword,
                                             $oldPassword, $changePasswordIndicator) {
        try {
            if ($changePasswordIndicator == "1") {
                if (!DBManager::isValidLogIn($oldEmail, $oldPassword)) {
                    throw new GeneralRegistryException("InvalidLoginException");
                }
            }
            DBManager::updateUserAccount($consultantId, $oldEmail, $newEmail,
                                         $newFirstName, $newLastName, $newPassword,
                                         $oldPassword, $changePasswordIndicator);                        
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return true;
    }

    public static function searchConsultants($searchGroupType, $consultantName,
                                             $email, $companyName, $servicesList,
                                             $pageNumber) {
        $consultants = array();
        try {
            $consultants = DBManager::searchConsultants($searchGroupType, $consultantName, $email, $companyName, $servicesList, $pageNumber);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return $consultants;
    }

    public static function sendRegistryCountsReportEmail($email) {
        try {
            $registryCount = AdminDBManager::getRegistryCounts();
            EmailManager::sendRegistryCountsReportEmail($email, $registryCount);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return true;
    }

    public static function sendMassCommunicationMessageEmail($title, $body) {
        try {
            $users = AdminDBManager::getAllUsersEmails();
            EmailManager::sendMassCommunicationMessageEmail($title, $body, $users);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return true;
    }

    public static function exportRegistryData() {
        $data = array();
        
        try {
            $usersInformation = AdminDBManager::exportRegistryData();
            for ($i = 0; $i < count($usersInformation); $i++) {
                $user = $usersInformation[$i];
                
                $data[$i] = array(
                                "Consultant Name" => $user->getFirstName() . " " . $user->getLastName(),
                                "Company Name" => $user->getCompanyName(),
                                "Telephone" => $user->getTelephone(),
                                "Email" => $user->getEmail(),
                                "Service Description" => $user->getServicesOfferedDescription(),
                                "K12 Client" => ($user->getK12ClienteleIndicator() == "1") ? "YES" : "NO",
                                "Higher Education Client" => ($user->getHigherEducationClienteleIndicator() == "1") ? "YES" : "NO",
                                "Published" => ($user->getProfileSubmittedIndicator() == "1") ? "YES" : "NO");
            }

        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $data;
    }

    public static function deleteService($serviceId) {
        try {
            MiscellaneousManager::deleteService($serviceId);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return true;
    }

    public static function addService($serviceName) {
        try {
            return AdminDBManager::addService($serviceName);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
    }

    public static function updateService($serviceId, $serviceName) {
        try {
            return MiscellaneousManager::updateService($serviceId, $serviceName);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
    }

    public static function exportConsultantServicesData() {
        $data = array();

        try {
            
            $data = AdminDBManager::exportConsultantServicesData();

            /*
            $usersInformation = AdminDBManager::exportConsultantServicesData();
            for ($i = 0; $i < count($usersInformation); $i++) {
                $user = $usersInformation[$i];

                $data[$i] = array(
                                "Consultant Name" => $user->getFirstName() . " " . $user->getLastName(),
                                "Company Name" => $user->getCompanyName(),
                                "Telephone" => $user->getTelephone(),
                                "Email" => $user->getEmail(),
                                "Service Description" => $user->getServicesOfferedDescription(),
                                "K12 Client" => ($user->getK12ClienteleIndicator() == "1") ? "YES" : "NO",
                                "Higher Education Client" => ($user->getHigherEducationClienteleIndicator() == "1") ? "YES" : "NO",
                                "Published" => ($user->getProfileSubmittedIndicator() == "1") ? "YES" : "NO");
            }
            */
            
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        }
        return $data;
    }

    public static function getConsultantsAccountInformation($firstName, $lastName, $email) {
        $consultants = array();
        try {
            $consultants = AdminDBManager::getConsultantsAccountInformation($firstName, $lastName, $email);
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return $consultants;
    }

    public static function updateUserEmailAccount($userId, $email) {
        try {
            AdminDBManager::updateUserEmailAccount($userId, $email);
            $user = AccountManager::getConsultant($email);
            EmailManager::sendPasswordRecoveryEmail($user->getFirstName(), $user->getLastName(), $user->getEmail(), $user->getPassword());
        } catch (GeneralRegistryException $e) {
            throw $e;
        } catch (DBException $e) {
            throw $e;
        } catch (DBConnectionException $e) {
            throw $e;
        } catch (Exception $e) {
            throw $e;
        }
        return true;
    }

}
?>
